package com.shiyanlou.upms.system.controller;

import com.shiyanlou.upms.common.utils.PageUtils;
import com.shiyanlou.upms.common.utils.Query;
import com.shiyanlou.upms.common.utils.R;
import com.shiyanlou.upms.system.domain.UpmsSystem;
import com.shiyanlou.upms.system.service.UpmsSystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 系统管理
 */
@Controller
@RequestMapping("/system")
public class UpmsSystemController {
    @Autowired
    private UpmsSystemService upmsSystemService;


    @GetMapping("/main")
    String UpmsSystem() {
        return "system/main";
    }


    @ResponseBody
    @GetMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<UpmsSystem> upmsSystemList = upmsSystemService.list(query);
        int total = upmsSystemService.count(query);
        PageUtils pageUtils = new PageUtils(upmsSystemList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    String add() {
        return "system/add";
    }


    @GetMapping("/edit/{systemId}")
    String edit(@PathVariable("systemId") Long systemId, Model model) {
        UpmsSystem upmsSystem = upmsSystemService.selectByPrimaryKey(systemId);
        model.addAttribute("upmsSystem", upmsSystem);
        return "system/edit";
    }

    @ResponseBody
    @PostMapping("/save")
    public R save(UpmsSystem upmsSystem) {
        if (upmsSystemService.insert(upmsSystem) > 0) {
            return R.ok();
        }
        return R.error();
    }

    @ResponseBody
    @RequestMapping("/update")
    public R update(UpmsSystem upmsSystem) {
        upmsSystemService.updateByPrimaryKeySelective(upmsSystem);
        return R.ok();
    }

    @PostMapping("/remove")
    @ResponseBody
    public R remove(Long systemId) {
        if (upmsSystemService.deleteByPrimaryKey(systemId) > 0) {
            return R.ok();
        }
        return R.error();
    }


    @PostMapping("/batchRemove")
    @ResponseBody
    public R remove(@RequestParam("ids[]") Integer[] systemIds) {
        upmsSystemService.batchRemove(systemIds);
        return R.ok();
    }
}
